package com.beiye.drivertransport.chart;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

public class ColumnView1 extends View {

    private float xScale;
    private float yScale;
    private float xPoint;
    private float yPoint;
    private float margin=30;
    private float marginX=20;
    private int itemX;

    private String[] transverse={"", "10", "20", "30", "40", "50", "60"};    //横列的刻度值数组
    private String[] vertical={"","12月", "11月", "10月", "9月", "8月","7月", "6月", "5月", "4月", "3月", "2月", "1月"};      //竖列的刻度值数组
    private int[] data={0,0,0,0,0,0,0,0,0,0,0,0,0};
    private int[] data1={0,0,0,0,0,0,0,0,0,0,0,0,0};
    private int[] data2={0,0,0,0,0,0,0,0,0,0,0,0,0};
    private int[] data3={0,0,0,0,0,0,0,0,0,0,0,0,0};
    private int color1,color2,color3,color4;



    private int mTotalWidth, mTotalHeight;
    private Paint paint,mPaint;

    public ColumnView1(Context context) {
        super(context);
        init(context);
    }

    public ColumnView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ColumnView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth=w;
        mTotalHeight=h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        init();
        // 画 X 轴
        canvas.drawLine(xPoint + 20, yPoint - 20, mTotalWidth+20, yPoint - 20, paint);
        // 画 Y 轴
        canvas.drawLine(xPoint + 20, yPoint - 20, xPoint + 20, 20, paint);

        // X轴坐标
        for (int i = 0; i <= (transverse.length - 1); i++) {
            paint.setTextAlign(Paint.Align.CENTER);
            float startX = xPoint+xPoint + i * xScale;
            canvas.drawText(itemX*i+"", startX - 10, mTotalHeight-20, paint);
        }

        for(int i=0;i<vertical.length;i++){
            float startY = yPoint - i * yScale+10;
            canvas.drawText(vertical[i], xPoint-20, startY, paint);
        }


        for(int i=1;i<vertical.length;i++){
            float startY = yPoint - i *yScale;
            paint.setColor(color1);
            float rightY=xPoint+marginX+(float) ((data[i]/(float)itemX*xScale)*percent);
            canvas.drawRect(xPoint+20, startY-2*marginX,rightY+2 , startY-marginX,paint); //矩形绘制
            canvas.drawCircle(rightY+2,startY-marginX/2*3,marginX/2,paint);//半圆绘制
            canvas.drawText(data[i]+"",rightY+20,startY-marginX-3,mPaint);

            paint.setColor(color2);
            float rightY1=xPoint+marginX+(float) ((data1[i]/(float)itemX*xScale)*percent);
            canvas.drawRect(xPoint+20, startY-marginX, rightY1+2, startY,paint);
            canvas.drawCircle(rightY1+2,startY-marginX/2,marginX/2,paint);
            canvas.drawText(data1[i]+"",rightY1+20,startY-3,mPaint);

            paint.setColor(color3);
            float rightY2=xPoint+marginX+(float) ((data2[i]/(float)itemX*xScale)*percent);
            canvas.drawRect(xPoint+20, startY,rightY2+2, startY+marginX,paint);
            canvas.drawCircle(rightY2+2,startY+marginX/2,marginX/2,paint);
            canvas.drawText(data2[i]+"",rightY2+20,startY+marginX-3,mPaint);

            paint.setColor(color4);
            float rightY3=xPoint+marginX+(float) ((data3[i]/(float)itemX*xScale)*percent);
            canvas.drawRect(xPoint+20, startY+marginX,rightY3+2, startY+2*marginX,paint);
            canvas.drawCircle(rightY3+2,startY+marginX+marginX/2,marginX/2,paint);
            canvas.drawText(data3[i]+"",rightY3+20,startY+2*marginX-3,mPaint);
        }
    }

    private void init() {
        paint=new Paint();
        mPaint=new Paint();

        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setTextSize(20f);

        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(20f);

        //计算原点坐标和单位长度
        xPoint = margin + marginX;
        yPoint = mTotalHeight - margin;

        xScale = (mTotalWidth - 4* margin - marginX) / (transverse.length-1);
        yScale = (mTotalHeight - 4 * margin) / (vertical.length - 1);
    }



    private float percent = 0f;
    private TimeInterpolator pointInterpolator = new DecelerateInterpolator();
    public void startAnimation(int duration){
        ValueAnimator mAnimator = ValueAnimator.ofFloat(0,1);
        mAnimator.setDuration(duration);
        mAnimator.setInterpolator(pointInterpolator);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                percent = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mAnimator.start();
    }

    public void setColor(int color1,int color2,int color3,int color4){
        this.color1=color1;
        this.color2=color2;
        this.color3=color3;
        this.color4=color4;
    }

    public void setData(int[] data,int[] data1,int[] data2,int[] data3){
        this.data=data;
        this.data1=data1;
        this.data2=data2;
        this.data3=data3;
        invalidate();
    }

    public void setItemX(int itemX){
        if(itemX<=5){
            this.itemX=5;
        }else if(itemX<=10){
            this.itemX=10;
        }else if(itemX<=20){
            this.itemX=20;
        }else if(itemX<=50){
            this.itemX=50;
        }else if(itemX<=100){
            this.itemX=100;
        }
    }
}

